﻿using System;
using System.IO.IsolatedStorage;
using System.Net;
using GalaSoft.MvvmLight;

namespace mal.WP7.FreeRemote.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        #region Fields
        private const string NetworkRemoteCodeKey = "networkRemoteCode";
        private const string BoxNumberKey = "boxNumber";

        private string networkRemoteCode;
        private int boxNumber = 1;

        private string urlFormat = "http://hd{0}.freebox.fr/pub/remote_control?code={1}&key={2}&guid={3}";
#if DEBUG
        private string devUrlFormat = "http://localhost:50283/WP7/FreeRemote/FreeRemoteControl.ashx?code={1}&key={2}&guid={3}&hd={0}";
#else
        private string devUrlFormat = "http://www.michaelalbaladejo.com/WP7/FreeRemote/FreeRemoteControl.ashx?code={1}&key={2}&guid={3}&hd={0}";
#endif

        //private WebClient client;
        #endregion

        #region Construction / Destruction
        public MainViewModel()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(NetworkRemoteCodeKey))
            {
                this.NetworkRemoteCode = (string)IsolatedStorageSettings.ApplicationSettings[NetworkRemoteCodeKey];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(BoxNumberKey))
            {
                this.BoxNumber = (int)IsolatedStorageSettings.ApplicationSettings[BoxNumberKey];
            }
        }
        #endregion

        #region enum, struct, class

        #endregion

        #region Properties
        public string NetworkRemoteCode
        {
            get
            {
                return this.networkRemoteCode;
            }

            set
            {
                if (this.networkRemoteCode != value)
                {
                    this.networkRemoteCode = value;
                    IsolatedStorageSettings.ApplicationSettings[NetworkRemoteCodeKey] = this.networkRemoteCode;
                    IsolatedStorageSettings.ApplicationSettings.Save();
                    RaisePropertyChanged("NetworkRemoteCode");
                }
            }
        }

        public int BoxNumber
        {
            get
            {
                return this.boxNumber;
            }

            set
            {
                if (this.boxNumber != value && value > 0)
                {
                    this.boxNumber = value;
                    IsolatedStorageSettings.ApplicationSettings[BoxNumberKey] = this.boxNumber;
                    IsolatedStorageSettings.ApplicationSettings.Save();
                    RaisePropertyChanged("BoxNumber");
                }
            }
        }
        #endregion

        #region Methods
        public void SendCommand(string command)
        {
            WebClient client = new WebClient();

            string url = string.Format(this.GetUrl(), this.BoxNumber, this.networkRemoteCode, command, Guid.NewGuid());
            Uri uri = new Uri(url, UriKind.Absolute);
            client.DownloadStringAsync(uri);
        }

        private string GetUrl()
        {
            if (this.networkRemoteCode == "000000")
            {
                return this.devUrlFormat;
            }
            else
            {
                return urlFormat;
            }
        }
        #endregion

        #region Events

        #endregion
    }
}
